home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / util / Mac F2C 1.3.sit / Mac F2C 1.3 / Mac F2C Libraries / libI77 Sources / err.c < prev    next >
Text File  |  1995-01-28  |  6KB  |  276 lines

  1. #ifndef NON_UNIX_STDIO
  2. #include "sys/types.h"
  3. #include "sys/stat.h"
  4. #endif
  5. #include "f2c.h"
  6. #include "fio.h"
  7. #include "fmt.h"    /* for struct syl */
  8. #include "rawio.h"    /* for fcntl.h, fdopen */
  9. #ifdef NON_UNIX_STDIO
  10. #ifdef KR_headers
  11. extern char *malloc();
  12. #else
  13. #undef abs
  14. #undef min
  15. #undef max
  16. #include "stdlib.h"
  17. #endif
  18. #endif
  19.  
  20. /*global definitions*/
  21. unit f__units[MXUNIT];    /*unit table*/
  22. flag f__init;    /*0 on entry, 1 after initializations*/
  23. cilist *f__elist;    /*active external io list*/
  24. flag f__reading;    /*1 if reading, 0 if writing*/
  25. flag f__cplus,f__cblank;
  26. char *f__fmtbuf;
  27. flag f__external;    /*1 if external io, 0 if internal */
  28. #ifdef KR_headers
  29. int (*f__doed)(),(*f__doned)();
  30. int (*f__doend)(),(*f__donewrec)(),(*f__dorevert)();
  31. int (*f__getn)(),(*f__putn)();    /*for formatted io*/
  32. #else
  33. int (*f__getn)(void),(*f__putn)(int);    /*for formatted io*/
  34. int (*f__doed)(struct syl*, char*, ftnlen),(*f__doned)(struct syl*);
  35. int (*f__dorevert)(void),(*f__donewrec)(void),(*f__doend)(void);
  36. #endif
  37. flag f__sequential;    /*1 if sequential io, 0 if direct*/
  38. flag f__formatted;    /*1 if formatted io, 0 if unformatted*/
  39. FILE *f__cf;    /*current file*/
  40. unit *f__curunit;    /*current unit*/
  41. int f__recpos;    /*place in current record*/
  42. int f__cursor,f__scale;
  43.  
  44. /*error messages*/
  45. char *F_err[] =
  46. {
  47.     "error in format",                /* 100 */
  48.     "illegal unit number",                /* 101 */
  49.     "formatted io not allowed",            /* 102 */
  50.     "unformatted io not allowed",            /* 103 */
  51.     "direct io not allowed",            /* 104 */
  52.     "sequential io not allowed",            /* 105 */
  53.     "can't backspace file",                /* 106 */
  54.     "null file name",                /* 107 */
  55.     "can't stat file",                /* 108 */
  56.     "unit not connected",                /* 109 */
  57.     "off end of record",                /* 110 */
  58.     "truncation failed in endfile",            /* 111 */
  59.     "incomprehensible list input",            /* 112 */
  60.     "out of free space",                /* 113 */
  61.     "unit not connected",                /* 114 */
  62.     "read unexpected character",            /* 115 */
  63.     "bad logical input field",            /* 116 */
  64.     "bad variable type",                /* 117 */
  65.     "bad namelist name",                /* 118 */
  66.     "variable not in namelist",            /* 119 */
  67.     "no end record",                /* 120 */
  68.     "variable count incorrect",            /* 121 */
  69.     "subscript for scalar variable",        /* 122 */
  70.     "invalid array section",            /* 123 */
  71.     "substring out of bounds",            /* 124 */
  72.     "subscript out of bounds",            /* 125 */
  73.     "can't read file",                /* 126 */
  74.     "can't write file",                /* 127 */
  75.     "'new' file exists",                /* 128 */
  76.     "can't append to file"                /* 129 */
  77. };
  78. #define MAXERR (sizeof(F_err)/sizeof(char *)+100)
  79.  
  80. #ifdef KR_headers
  81. f__canseek(f) FILE *f; /*SYSDEP*/
  82. #else
  83. f__canseek(FILE *f) /*SYSDEP*/
  84. #endif
  85. {
  86. #ifdef NON_UNIX_STDIO
  87.     return !isatty(fileno(f));
  88. #else
  89.     struct stat x;
  90.  
  91.     if (fstat(fileno(f),&x) < 0)
  92.         return(0);
  93. #ifdef S_IFMT
  94.     switch(x.st_mode & S_IFMT) {
  95.     case S_IFDIR:
  96.     case S_IFREG:
  97.         if(x.st_nlink > 0)    /* !pipe */
  98.             return(1);
  99.         else
  100.             return(0);
  101.     case S_IFCHR:
  102.         if(isatty(fileno(f)))
  103.             return(0);
  104.         return(1);
  105. #ifdef S_IFBLK
  106.     case S_IFBLK:
  107.         return(1);
  108. #endif
  109.     }
  110. #else
  111. #ifdef S_ISDIR
  112.     /* POSIX version */
  113.     if (S_ISREG(x.st_mode) || S_ISDIR(x.st_mode)) {
  114.         if(x.st_nlink > 0)    /* !pipe */
  115.             return(1);
  116.         else
  117.             return(0);
  118.         }
  119.     if (S_ISCHR(x.st_mode)) {
  120.         if(isatty(fileno(f)))
  121.             return(0);
  122.         return(1);
  123.         }
  124.     if (S_ISBLK(x.st_mode))
  125.         return(1);
  126. #else
  127.     Help! How does fstat work on this system?
  128. #endif
  129. #endif
  130.     return(0);    /* who knows what it is? */
  131. #endif
  132. }
  133.  
  134.  void
  135. #ifdef KR_headers
  136. f__fatal(n,s) char *s;
  137. #else
  138. f__fatal(int n, char *s)
  139. #endif
  140. {
  141.     if(n<100 && n>=0) perror(s); /*SYSDEP*/
  142.     else if(n >= (int)MAXERR || n < -1)
  143.     {    fprintf(stderr,"%s: illegal error number %d\n",s,n);
  144.     }
  145.     else if(n == -1) fprintf(stderr,"%s: end of file\n",s);
  146.     else
  147.         fprintf(stderr,"%s: %s\n",s,F_err[n-100]);
  148.     if (f__curunit) {
  149.         fprintf(stderr,"apparent state: unit %d ",f__curunit-f__units);
  150.         fprintf(stderr, f__curunit->ufnm ? "named %s\n" : "(unnamed)\n",
  151.             f__curunit->ufnm);
  152.         }
  153.     else
  154.         fprintf(stderr,"apparent state: internal I/O\n");
  155.     if (f__fmtbuf)
  156.         fprintf(stderr,"last format: %s\n",f__fmtbuf);
  157.     fprintf(stderr,"lately %s %s %s %s",f__reading?"reading":"writing",
  158.         f__sequential?"sequential":"direct",f__formatted?"formatted":"unformatted",
  159.         f__external?"external":"internal");
  160.     sig_die(" IO", 1);
  161. }
  162. /*initialization routine*/
  163.  VOID
  164. f_init(Void)
  165. {    unit *p;
  166.  
  167.     f__init=1;
  168.     p= &f__units[0];
  169.     p->ufd=stderr;
  170.     p->useek=f__canseek(stderr);
  171. #ifdef NON_UNIX_STDIO
  172.     setbuf(stderr, (char *)malloc(BUFSIZ));
  173. #else
  174.     stderr->_flag &= ~_IONBF;
  175. #endif
  176.     p->ufmt=1;
  177.     p->uwrt=1;
  178.     p = &f__units[5];
  179.     p->ufd=stdin;
  180.     p->useek=f__canseek(stdin);
  181.     p->ufmt=1;
  182.     p->uwrt=0;
  183.     p= &f__units[6];
  184.     p->ufd=stdout;
  185.     p->useek=f__canseek(stdout);
  186.     p->ufmt=1;
  187.     p->uwrt=1;
  188. }
  189. #ifdef KR_headers
  190. f__nowreading(x) unit *x;
  191. #else
  192. f__nowreading(unit *x)
  193. #endif
  194. {
  195.     long loc;
  196.     int ufmt;
  197.     extern char *f__r_mode[];
  198.  
  199.     if (!x->ufnm)
  200.         goto cantread;
  201.     ufmt = x->ufmt;
  202.     loc=ftell(x->ufd);
  203.     if(freopen(x->ufnm,f__r_mode[ufmt],x->ufd) == NULL) {
  204.  cantread:
  205.         errno = 126;
  206.         return(1);
  207.         }
  208.     x->uwrt=0;
  209.     (void) fseek(x->ufd,loc,SEEK_SET);
  210.     return(0);
  211. }
  212. #ifdef KR_headers
  213. f__nowwriting(x) unit *x;
  214. #else
  215. f__nowwriting(unit *x)
  216. #endif
  217. {
  218.     long loc;
  219.     int ufmt;
  220.     extern char *f__w_mode[];
  221. #ifndef NON_UNIX_STDIO
  222.     int k;
  223. #endif
  224.  
  225.     if (!x->ufnm)
  226.         goto cantwrite;
  227.     ufmt = x->ufmt;
  228. #ifdef NON_UNIX_STDIO
  229.     ufmt |= 2;
  230. #endif
  231.     if (x->uwrt == 3) { /* just did write, rewind */
  232. #ifdef NON_UNIX_STDIO
  233.         if (!(f__cf = x->ufd =
  234.                 freopen(x->ufnm,f__w_mode[ufmt],x->ufd)))
  235. #else
  236.         if (close(creat(x->ufnm,0666)))
  237. #endif
  238.             goto cantwrite;
  239.         }
  240.     else {
  241.         loc=ftell(x->ufd);
  242. #ifdef NON_UNIX_STDIO
  243.         if (!(f__cf = x->ufd =
  244.             freopen(x->ufnm, f__w_mode[ufmt], x->ufd)))
  245. #else
  246.         if (fclose(x->ufd) < 0
  247.         || (k = x->uwrt == 2 ? creat(x->ufnm,0666)
  248.                      : open(x->ufnm,O_WRONLY)) < 0
  249.         || (f__cf = x->ufd = fdopen(k,f__w_mode[ufmt])) == NULL)
  250. #endif
  251.             {
  252.             x->ufd = NULL;
  253.  cantwrite:
  254.             errno = 127;
  255.             return(1);
  256.             }
  257.         (void) fseek(x->ufd,loc,SEEK_SET);
  258.         }
  259.     x->uwrt = 1;
  260.     return(0);
  261. }
  262.  
  263.  int
  264. #ifdef KR_headers
  265. err__fl(f, m, s) int f, m; char *s;
  266. #else
  267. err__fl(int f, int m, char *s)
  268. #endif
  269. {
  270.     if (!f)
  271.         f__fatal(m, s);
  272.     if (f__doend)
  273.         (*f__doend)();
  274.     return errno = m;
  275.     }
  276.